package com.wedata.tikhub;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;


/**
 * https://docs.tikhub.io/186826153e0
 */
public class DouyinWebAPI extends TikhubBase {



    public static void main(String[] args) {
        //fetchUserProfileByUid(); //可查询
        //getHotVideoList();
        getVideoComments();
        //getVideoCommentReplies();
    }

    /**
     * 获取单个视频评论数据
     * 调一次费用：0.001
     * 重复调用重复收费：0.001
     */
    public static void getVideoComments(){
        String url = "https://api.tikhub.dev/api/v1/douyin/web/fetch_video_comments?aweme_id=7500556866059488522&cursor=0&count=100";
        String response = doGet(url);
        //System.out.println(response);
        JSONObject jsonObject = new JSONObject(response);
        System.out.println(jsonObject.get("params"));
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray comments = data.getJSONArray("comments");
        System.out.println(comments.size());
        for(int i=0;i<comments.size();i++){
            JSONObject comment = comments.getJSONObject(i);
            int create_time =comment.getInt("create_time");
            String text = comment.getStr("text");
            System.out.println(timeToStr(create_time)+" : "+text);
        }
    }
    /**
     * 获取指定视频的评论回复数据
     */
    public static void getVideoCommentReplies(){
        //comment_id非必传，但是必须传一个空字符串
        String url = BASE_URL + "api/v1/douyin/web/fetch_video_comment_replies?item_id=7500556866059488522&comment_id=&cursor=0&count=20";
        String response = doGet(url);
        System.out.println(response);
        //{"code":200,"request_id":"6c122161-22f4-43a3-90a4-ca7d974490ed","message":"Request successful. This request will incur a charge.","message_zh":"请求成功，本次请求将被计费。","support":"Discord: https://discord.gg/aMEAS8Xsvz","time":"2025-10-20 18:46:03","time_stamp":1761011163,"time_zone":"America/Los_Angeles","docs":"https://api.tikhub.io/#/Douyin-Web-API/fetch_video_comments_reply_api_v1_douyin_web_fetch_video_comment_replies_get","cache_message":"This request will be cached. You can access the cached result directly using the URL below, valid for 24 hours.","cache_message_zh":"本次请求将被缓存，你可以使用下面的 URL 直接访问缓存结果，有效期为 24 小时。","cache_url":"https://cache.tikhub.io/api/v1/cache/public/6c122161-22f4-43a3-90a4-ca7d974490ed?sign=9f67e649a0b1a671e76c23b97371d5868c4577820104f63f4f62f23216a4479d","router":"/api/v1/douyin/web/fetch_video_comment_replies","params":{"item_id":"7500556866059488522","comment_id":"","cursor":"0","count":"20"},"data":{"status_code":0,"comments":null,"cursor":0,"has_more":0,"total":0,"extra":{"now":1761011163000,"fatal_item_ids":null,"scenes":null},"log_pb":{"impr_id":"202510210946035FD05A7DE1A2E23FCA47"}}}
    }

    /**
     * 获取热门视频列表(关键词搜索)
     */
    public static void getHotVideoList() {
        String url = BASE_URL + "api/v1/douyin/web/fetch_general_search_result"
                + "?keyword=李维刚" + "&offset=0&count=20&sort_type=2&publish_time=0&filter_duration=0&content_type=1";
        String response = doGet(url);
        System.out.println(response);
    }

    /**
     * 获取用户信息
     * 调一次费用：0.001
     */
    public static void fetchUserProfileByUid() {
        String url = BASE_URL + "api/v1/douyin/web/fetch_user_profile_by_uid"
                + "?uid=71510504205";
        String response = doGet(url);
        System.out.println(response);
    }

}

